package js.lee.jvli.kit;

import js.lee.jvli.config.Configuration;
import js.lee.jvli.config.MailNode;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class EmailKit {
    public static boolean sendMail(String to, String title, String content) {
        Configuration config = Configuration.getInstance();
        return sendMail(config.getMail(), to, title, content);
    }

    public static boolean sendMail(MailNode config, String to, String title, String content) {
        try {
            Email email = new SimpleEmail();
            email.setHostName(config.getServer());
            email.setSmtpPort(config.getPort());
            email.setAuthenticator(new DefaultAuthenticator(config.getUser(), config.getPassword()));
            email.setSSLOnConnect(config.isSsl());
            email.setFrom(config.getFrom());
            email.setSubject(title);
            email.setMsg(content);
            email.addTo(to);
            email.send();
            return true;
        }
        catch (EmailException e) {
            e.printStackTrace();
        }
        return false;
    }
}
